<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
    //
    protected $table="user_team";
    protected $guarded = [];
    const START_TIME = '2019-05-20 00:00:00';
    const END_TIME = '2019-06-30 00:00:00';

    public static function createTeamCode($openCode){
        return time().$openCode;
    }

    public static function createdTeam(User $user,User $parent){
        if (strtotime(Team::START_TIME)<=time() || strtotime(Team::END_TIME)>=time()){
            $team = Team::where("user_id",$parent->id)->where("role","leader")->first();
            if ($team){
                $teamUser = new Team();
                $teamUser->team_code=$team->team_code;
                $teamUser->user_id = $user->id;
                $teamUser->mobile = $user->mobile;
                $teamUser->is_recharge = 0;
                $teamUser->role = "member";
                $teamUser->remark = "未充值";
                $teamUser->status = 0;
                $teamUser->created_at = Carbon::now();
                $teamUser->save();
            }
        }
    }

    public function getMobileAttribute(){
        return substr_replace($this->attributes['mobile'],'****',3,4);
    }
}
